package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IDeliveryDetailService;

import javax.annotation.Resource;

/**
 * 采购单详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@Service
public class DeliveryDetailServiceImpl implements IDeliveryDetailService {
    @Resource
    private DeliveryDetailMapper deliveryDetailMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private SpecMapper specMapper;
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private DeliveryMapper deliveryMapper;

    /**
     * 查询采购单详情
     *
     * @param id 采购单详情主键
     * @return 采购单详情
     */
    @Override
    public DeliveryDetail selectDeliveryDetailById(Long id) {
        DeliveryDetail deliveryDetail = deliveryDetailMapper.selectDeliveryDetailById(id);
        List<DeliveryDetail> list = new ArrayList<DeliveryDetail>();
        list.add(deliveryDetail);
        format(list);
        return list.get(0);
    }

    /**
     * 查询采购单详情列表
     *
     * @param deliveryDetail 采购单详情
     * @return 采购单详情
     */
    @Override
    public List<DeliveryDetail> selectDeliveryDetailList(DeliveryDetail deliveryDetail) {

        if (deliveryDetail.getParams() != null) {
            if (deliveryDetail.getParams().containsKey("sendTimeStart") && deliveryDetail.getParams().containsKey("sendTimeEnd")) {
                if (deliveryDetail.getParams().get("sendTimeStart") != null && !"".equals(deliveryDetail.getParams().get("sendTimeStart"))
                        && deliveryDetail.getParams().get("sendTimeEnd") != null && !"".equals(deliveryDetail.getParams().get("sendTimeEnd"))) {
                    deliveryDetail.getParams().put("sendTimeStart", deliveryDetail.getParams().get("sendTimeStart") + " 00:00:00");
                    deliveryDetail.getParams().put("sendTimeEnd", deliveryDetail.getParams().get("sendTimeEnd") + " 23:59:59");
                }

            }
        }

        List<DeliveryDetail> detailList = deliveryDetailMapper.selectDeliveryDetailList(deliveryDetail);


        format(detailList);

        return detailList;
    }

    private void format(List<DeliveryDetail> detailList) {

        Map<Long, Type> typeMap = typeMapper.selectTypeList(new Type()).stream().collect(Collectors.toMap(Type::getId, t -> t));
        Map<Long, Spec> specMap = specMapper.selectSpecList(new Spec()).stream().collect(Collectors.toMap(Spec::getId, t -> t));
        Map<Long, Brand> brandMap = brandMapper.selectBrandList(new Brand()).stream().collect(Collectors.toMap(Brand::getId, t -> t));
        Map<Long, SysDept> schoolMap = sysDeptMapper.selectDeptList(new SysDept() {{
            setType(1);
        }}).stream().collect(Collectors.toMap(SysDept::getDeptId, t -> t));
        Map<Long, SysDept> companyMap = sysDeptMapper.selectDeptList(new SysDept() {{
            setType(0);
        }}).stream().collect(Collectors.toMap(SysDept::getDeptId, t -> t));

        detailList.forEach(item -> {
            if (item.getTypeId() != null) {
                Type type = typeMap.get(item.getTypeId());
                if (type != null) {
                    item.setTypeName(type.getName());
                }
            }
            if (item.getSpecId() != null) {
                Spec spec = specMap.get(item.getSpecId());
                if (spec != null) {
                    item.setSpecName(spec.getName());
                }
            }
            if (item.getBrandId() != null) {
                Brand brand = brandMap.get(item.getBrandId());
                if (brand != null) {
                    item.setBrandName(brand.getName());
                }
            }
            if (item.getSchoolId() != null) {
                SysDept school = schoolMap.get(item.getSchoolId());
                if (school != null) {
                    item.setSchoolName(school.getDeptName());
                }
            }
            if (item.getDeliveryId() != null) {
                Delivery delivery = deliveryMapper.selectDeliveryById(item.getDeliveryId());
                if (delivery != null) {
                    item.setSendTime(delivery.getSendTime());
                    SysDept sysDept = companyMap.get(delivery.getDeptId());
                    if (sysDept != null) {
                        item.setCompanyName(sysDept.getDeptName());
                    }
                }
            }
        });
    }

    /**
     * 新增采购单详情
     *
     * @param deliveryDetail 采购单详情
     * @return 结果
     */
    @Override
    public int insertDeliveryDetail(DeliveryDetail deliveryDetail) {
        deliveryDetail.setCreateTime(DateUtils.getNowDate());
        return deliveryDetailMapper.insertDeliveryDetail(deliveryDetail);
    }

    /**
     * 修改采购单详情
     *
     * @param deliveryDetail 采购单详情
     * @return 结果
     */
    @Override
    public int updateDeliveryDetail(DeliveryDetail deliveryDetail) {
        deliveryDetail.setUpdateTime(DateUtils.getNowDate());
        return deliveryDetailMapper.updateDeliveryDetail(deliveryDetail);
    }

    /**
     * 批量删除采购单详情
     *
     * @param ids 需要删除的采购单详情主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryDetailByIds(Long[] ids) {
        return deliveryDetailMapper.deleteDeliveryDetailByIds(ids);
    }

    /**
     * 删除采购单详情信息
     *
     * @param id 采购单详情主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryDetailById(Long id) {
        return deliveryDetailMapper.deleteDeliveryDetailById(id);
    }
}
